const path = require("path");
const isPro = process.env.NODE_ENV === "production";
// const publicPath = isPro ? '/login' : '/login'
const devtool = isPro ? false : "cheap-module-eval-source-map";

function resolve(dir) {
  return path.join(__dirname, dir);
}
// let proxy = {};

// Object.keys(config).forEach((key) => {
//   let opts = config[key];
//   proxy[opts.path] = opts;
//   delete opts.path;
// });

module.exports = {
  publicPath: process.env.VUE_APP_BASEURL || "/login/",
  // 开发时启动eslint校验
  lintOnSave: !isPro,
  productionSourceMap: false,
  configureWebpack() {
    return {
      devtool,
      resolve: {
        alias: {
          "@": resolve("src"),
          public: resolve("public"),
          config: resolve("config"),
        },
      },
    };
  },
  chainWebpack(config) {
    // 去掉元素间的空格
    config.module
      .rule("vue")
      .use("vue-loader")
      .loader("vue-loader")
      .tap((options) => {
        options.compilerOptions.preserveWhitespace = true;
        return options;
      })
      .end();
    config.when(isPro, (config) => {
      config.optimization.splitChunks({
        chunks: "all",
        cacheGroups: {
          libs: {
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: "initial", // only package third parties that are initially dependent
          },
          echarts: {
            name: "chunk-echarts",
            test: /[\\/]node_modules[\\/]_?echarts(.*)/,
            priority: 15,
          },
          commons: {
            name: "chunk-commons",
            test: resolve("src/components"), // can customize your rules
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true,
          },
        },
      });
      config.optimization.runtimeChunk("single");
    });
  },
  css: {
    loaderOptions: {
      sass: {
        prependData: `@import "~@/styles/variables/_variables.sass"`,
      },
      scss: {
        prependData: `@import "~@/styles/variables/_variables.scss";`,
      },
    },
  },
  devServer: {
    https: false,
    port: 9091,
    // 启动eslint格式校验弹窗
    // overlay: {
    //   warnings: false,
    //   errors: true
    // },
    proxy: {
      [process.env.VUE_APP_OAUTH2_PATH]: {
        target: process.env.VUE_APP_OAUTH2_URL,
        ws: false,
        changeOrigin: true,
      },
    },
  },
  transpileDependencies: ["vue-echarts", "resize-detector"],
};
